home *** CD-ROM | disk | FTP | other *** search
- ////////////////////////////////////////////////////////////////////
- //
- // CoercescptToText.c written by Donald O. Olson
- // A simple Coercion Scripting Addition written
- // to illustrate writing Scripting Additions.
- //
- // Copyright ®1993 Donald O. Olson
- // All rights reserved.
- //
- //////////////////////////////////////////////////////////////////
-
- #include <Memory.h>
- #include <Fonts.h>
- #include <OSEvents.h>
- #include <Menus.h>
- #include <Processes.h>
- #include <String.h>
- #include <Resources.h>
- #include <Packages.h>
- #include <AppleEvents.h>
- #include <Errors.h>
- #include <GestaltEqu.h>
- #include <Files.h>
- #include <OSA.h>
-
- #define typeStyledText 'STXT'
-
- //////////////////////////////////////////////////////////////////
- //
- // main()
- // This is the interface for a coerce from descriptor
- // coercion. Remember to declare it 'pascal'!!!
- //
- //////////////////////////////////////////////////////////////////
-
- pascal OSErr main( AEDesc *scriptDesc, DescType toType,
- long refcon, AEDesc *resultDesc)
- {
- OSErr theErr = noErr;
- OSAError theOSAErr;
- ComponentInstance gASComponent = 0;
- OSAID resultingID = 0;
- long modeFlags = 0;
-
- /*
- Open an instantiation of the
- Generic Scripting Component
- */
-
- gASComponent = OpenDefaultComponent(kOSAComponentType,
- kOSAGenericScriptingComponentSubtype);
-
- /* Checking errors here! */
- if(((long)gASComponent == (long)badComponentInstance) ||
- ((long)gASComponent == (long)badComponentSelector)) {
- theErr = invalidComponentID;
- goto CLEANUP; // Yea! A valid use for a 'goto'!!
- }
-
- /* Load script in scriptDesc into a scriptID */
- theOSAErr = OSALoad( gASComponent,
- scriptDesc,
- modeFlags,
- &resultingID);
-
-
- if( theOSAErr != noErr) {
- theErr = theOSAErr;
- goto CLEANUP;
- }
-
- /*
- Now get the source. Since AppleScript can coerce any
- of the various text forms to a text object (which is
- what we claim to be returning, let's just return the
- styled text and let AppleScript do the secondary
- coercion for us.
- */
-
- theOSAErr = OSAGetSource( gASComponent,
- resultingID,
- typeStyledText,
- resultDesc);
-
- if( theOSAErr != noErr) theErr = theOSAErr;
-
- CLEANUP:;
- if(resultingID != 0) OSADispose(gASComponent, resultingID);
- if(gASComponent != 0) CloseComponent(gASComponent);
-
- return theErr;
- }
-